package com.exampl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exampl.dao.DishDao;
import com.exampl.domain.Dish;
import com.exampl.domain.DishFlavor;
import com.exampl.domain.Setmeal;
import com.exampl.domain.SetmealDish;
import com.exampl.dto.DishDto;
import com.exampl.service.DishFlavorService;
import com.exampl.service.DishService;
import com.exampl.service.SetmealDishService;
import com.exampl.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    SetmealService setmealService;

    @Override
    // 开启事务
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品
        this.save(dishDto);
        // 获取菜品id，当保存菜品时得到的id值会自动返回给dishDto的id变量
        Long id = dishDto.getId();
        // 存储id到口味集合中
        List<DishFlavor> list = dishDto.getFlavors();
        list = list.stream().peek((data) -> data.setDishId(id)).collect(Collectors.toList());
        // 保存菜品口味
        dishFlavorService.saveBatch(list);
    }

    // 回显数据
    @Override
    public DishDto getDishDto(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        // 将dish中的数据赋值给dishDto
        BeanUtils.copyProperties(dish, dishDto);
        // 获取DishDto对象
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<DishFlavor>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavor = dishFlavorService.list(lqw);
        dishDto.setFlavors(flavor);
        return dishDto;
    }

    @Override
    // 开启事务
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 更新菜品
        this.updateById(dishDto);
        // 清除口味
        Long id = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<DishFlavor>();
        lqw.eq(DishFlavor::getDishId, id);
        dishFlavorService.remove(lqw);
        // 重新添加口味
        List<DishFlavor> list = dishDto.getFlavors();
        list = list.stream().peek((data) -> data.setDishId(id)).collect(Collectors.toList());
        dishFlavorService.saveBatch(list);
    }

    @Override
    public boolean isExist(Long id) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<SetmealDish>();
        lqw.eq(SetmealDish::getDishId, id);
        int count = setmealDishService.count(lqw);
        return count > 0;
    }

    @Override
    public void setMealExist(Long id) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<SetmealDish>();
        lqw.eq(SetmealDish::getDishId, id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        List<Setmeal> setmeals = list.stream().map((data) -> {
            Setmeal setmeal = new Setmeal();
            Long aLong = data.getSetmealId();
            setmeal.setId(aLong);
            setmeal.setStatus(0);
            return setmeal;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(setmeals);
    }
}
